package com.lqb.webScaffold.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.lqb.webScaffold.utils.MinioUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/files")
public class FileController {

    @Autowired
    private MinioUtils minioUtils;

    @PostMapping("/upload")
    public R uploadFile(@RequestParam("file") MultipartFile file) {
        String fileName = minioUtils.uploadFile(file);
        if( fileName!=null ) return R.ok(fileName).setMsg("File uploaded successfully");
        else return R.failed("Failed to upload file");
    }

    @GetMapping("/{fileName}")
    public void downloadFile(@PathVariable("fileName") String fileName, HttpServletResponse response) throws Exception {
        minioUtils.downloadFile(fileName, response);
    }
}

